package fr.toto.settings;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;

import com.kokotchy.betaSeriesAPI.model.SubtitleLanguage;

import fr.toto.constantes.Constantes;

public class SettingsBean implements Constantes {
	
	private static String P_LOGIN = "user.login";
	private static String P_TOKEN = "user.token";
	private static String P_SCANDIR = "path.scan";
	private static String P_RECURSIVESCAN = "path.scan.isrecursif";
	private static String P_DESTDIR = "path.dest";
	private static String P_READDIR = "path.read";
	private static String P_FORMAT = "path.format";
	private static String P_WEBUI = "webui.actif";
	private static String P_PORTWEBUI = "webui.port";
	private static String P_SUBLANG = "sub.langue";
	private static String P_EXTVIDEO = "path.ext";
	private static String P_SUBREGEXWITH = "sub.accept.regex";
	private static String P_SUBREGEXWITHOUT = "sub.refuse.regex";
	private static String P_DEBUG = "debug";
	
	private String login;
	private String token;

	private ArrayList<Path> scanDir;
	private boolean recursiveScan;
	private Path destDir;
	private ArrayList<Path> readDir;
	
	private String format;
	
	private boolean webUI;
	private int portUI;
	
	private SubtitleLanguage subLang;
	
	private ArrayList<String> extVideo;
	
	private String SubRegexWith;
	private String SubRegexWithout;
	
	private boolean debug;
	
	public SettingsBean() {
	}
	
	public void loadFromXML(InputStream in) throws InvalidPropertiesFormatException, IOException {
		Properties p = new Properties();
		p.loadFromXML(in);
		
		login = p.getProperty(P_LOGIN);
		token = p.getProperty(P_TOKEN);
		
		scanDir = getListPath(p, P_SCANDIR);
		recursiveScan = "true".equalsIgnoreCase(p.getProperty(P_RECURSIVESCAN,"false"));
		destDir = Paths.get(p.getProperty(P_DESTDIR));
		readDir = getListPath(p, P_READDIR);
		
		format = p.getProperty(P_FORMAT);
		
		webUI = "true".equalsIgnoreCase(p.getProperty(P_WEBUI,"false"));
		
		portUI = Integer.parseInt(p.getProperty(P_PORTWEBUI,"0"));
		
		extVideo  = getListString(p, P_EXTVIDEO);
		
		subLang = SubtitleLanguage.valueOf(p.getProperty(P_SUBLANG,"UNKNOWN"));

		SubRegexWith = p.getProperty(P_SUBREGEXWITH);
		SubRegexWithout = p.getProperty(P_SUBREGEXWITHOUT);
		
		debug = "true".equalsIgnoreCase(p.getProperty(P_DEBUG,"false"));
	}
	
	public void storeToXML(OutputStream out) throws IOException {
		Properties p = new Properties();
		
		if(login != null && !login.isEmpty())
			p.setProperty(P_LOGIN,login);
		if(token != null && !token.isEmpty())
			p.setProperty(P_TOKEN,token);
		
		if(scanDir != null && !scanDir.isEmpty())
			p.putAll(setProperties(P_SCANDIR,scanDir));
		p.setProperty(P_RECURSIVESCAN, String.valueOf(recursiveScan));
		if(destDir != null)
			p.setProperty(P_DESTDIR, destDir.toString());
		if(readDir != null && !readDir.isEmpty())
			p.putAll(setProperties(P_READDIR,readDir));
		
		if(format != null && !format.isEmpty())
			p.setProperty(P_FORMAT,format);
		
		p.setProperty(P_WEBUI, String.valueOf(webUI));
		if(portUI != 0)
			p.setProperty(P_PORTWEBUI, String.valueOf(portUI));

		if(subLang != null && !subLang.equals(SubtitleLanguage.UNKNOWN))
			p.setProperty(P_SUBLANG, String.valueOf(subLang));
		
		if(extVideo != null && !extVideo.isEmpty())
			p.putAll(setProperties2(P_EXTVIDEO,extVideo));
		
		if(SubRegexWith != null && !SubRegexWith.isEmpty())
			p.setProperty(P_SUBREGEXWITH,SubRegexWith);
		if(SubRegexWithout != null && !SubRegexWithout.isEmpty())
			p.setProperty(P_SUBREGEXWITHOUT,SubRegexWithout);
		
		p.setProperty(P_DEBUG, String.valueOf(debug));
		
		p.storeToXML(out,"",CharsetName);		
	}
	
	private ArrayList<String> getListString(Properties p, String property) {
		ArrayList<String> al = new ArrayList<String>();
		String s;
		for(int i = 0; (s = p.getProperty(property +"."+i)) != null ; i++ ) {
			al.add(s);
		}
		return al;
	}
	
	private ArrayList<Path> getListPath(Properties p, String property) {
		ArrayList<Path> al = new ArrayList<Path>();
		String s;
		for(int i = 0; (s = p.getProperty(property +"."+i)) != null ; i++ ) {
			Path path = Paths.get(s);
			al.add(path);
		}
		return al;
	}
	
	
	private Properties setProperties(String property,List<Path> l) {
		Properties p = new Properties();
		int i = 0;
		for(Path path:l) {
			p.setProperty(property +"."+ (i++), path.toString());
		}
		return p;
	} 
	
	private Properties setProperties2(String property, List<String> l) {
		Properties p = new Properties();
		int i = 0;
		for(String s:l) {
			p.setProperty(property +"."+ (i++),s);
		}
		return p;
	}

	public void setLogin(String l) {
		login = l;
	}
	
	public String getLogin() {
		return login;
	}
	
	public void setToken(String t) {
		token = t;
	}
	
	public String getToken() {
		return token;
	}
	
	public void setScanDir(Collection<Path> sd) {
		scanDir = new ArrayList<Path>(sd);
	}
	
	public List<Path> getScanDir() {
		return scanDir;
	}
	
	public void setDestDir(Path p) {
		destDir = p;
	}
	
	public Path getDestDir() {
		return destDir;
	}
	
	public void setReadDir(Collection<Path> sd) {
		readDir = new ArrayList<Path>(sd);
	}
	
	public List<Path> getReadDir() {
		ArrayList<Path> alp = new ArrayList<Path>(readDir);
		alp.add(destDir);
		return alp;
	}
	
	public List<Path> getReadList() {
		return readDir;
	}
	
	public void setFormatFile(String f) {
		format = f;
	}
	
	public String getFormatFile() {
		return format;
	}
	
	public void setWebUI(boolean b) {
		webUI = b;
	}
	
	public boolean useWebUI() {
		return webUI;
	}
	
	public void setWebUIPort(int p) {
		portUI = p;
	}

	public int getPortWebUI() {
		return portUI;
	}

	public List<String> getExtVideo() {
		return extVideo;
	}

	public void setExtVideo(Collection<String> exts) {
		extVideo = new ArrayList<String>(exts);
	}

	public String getSubRegexWith() {
		return SubRegexWith;
	}

	public void setSubRegexWith(String subRegexWith) {
		SubRegexWith = subRegexWith;
	}

	public String getSubRegexWithout() {
		return SubRegexWithout;
	}

	public void setSubRegexWithout(String subRegexWithout) {
		SubRegexWithout = subRegexWithout;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public boolean isRecursiveScan() {
		return recursiveScan;
	}

	public void setRecursiveScan(boolean recursiveScan) {
		this.recursiveScan = recursiveScan;
	}

	public SubtitleLanguage getSubLang() {
		return subLang;
	}

	public void setSubLang(SubtitleLanguage subLang) {
		this.subLang = subLang;
	}

	public String getExtsRegex() {
		String s = "";
		for(String ext : extVideo) {
			s += ext.toLowerCase() + " " + ext.toUpperCase() + " " ;
		}
		return "(" + s.trim().replace(' ', '|') + ")$";				
	}

	public String getExtsGlob() {
		String s = "";
		for(String ext : extVideo) {
			s += ext.toLowerCase() + " " + ext.toUpperCase() + " " ;
		}
		return ".{" + s.trim().replace(' ', ',') + "}";		
	}
}
